package com.example.speeddating.interceptor;
import com.example.speeddating.utils.JwtTokenUtil;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Component
public class AdminVerifyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getMethod().equals("OPTIONS")){
            System.out.println("预检");
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        response.setCharacterEncoding("utf-8");
        String token = request.getHeader("token");
        if (token!=null){
//            boolean result = AdminVerify.verify(token);
            boolean b = JwtTokenUtil.validateToken(token);
            if (b){
                return true;
            }
        }
        response.setContentType("application/json; charset=utf-8");
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("msg","您无权访问");
            map.put("code","500");
            response.getWriter().append(map.toString());
//            System.out.println("非管理员");
        } catch (Exception e) {
            return false;
        }
        return false;
    }

}
